package cn.iocoder.yudao.module.lfd.service.schedule;

import cn.hutool.core.date.Week;
import cn.iocoder.yudao.module.lfd.dal.dataobject.schedule.NickScheduleDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.lfd.controller.admin.schedule.vo.*;
import cn.iocoder.yudao.module.lfd.dal.dataobject.schedule.ScheduleDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.lfd.convert.schedule.ScheduleConvert;
import cn.iocoder.yudao.module.lfd.dal.mysql.schedule.ScheduleMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.lfd.enums.ErrorCodeConstants.*;

/**
 * 排班 Service 实现类
 *
 * @author 陈星星
 */
@Service
@Validated
public class ScheduleService {

    @Resource
    private ScheduleMapper scheduleMapper;

   
    public Long createSchedule(ScheduleCreateReqVO createReqVO) {
        // 插入
        ScheduleDO schedule = ScheduleConvert.INSTANCE.convert(createReqVO);
        String name = createReqVO.getScheduleDate().getDayOfWeek().name();
        String week = Week.of(name).toChinese();
        schedule.setWeek(week);
        scheduleMapper.insert(schedule);
        // 返回
        return schedule.getId();
    }

   
    public void updateSchedule(ScheduleUpdateReqVO updateReqVO) {
        // 校验存在
        validateScheduleExists(updateReqVO.getId());
        // 更新
        ScheduleDO updateObj = ScheduleConvert.INSTANCE.convert(updateReqVO);
        scheduleMapper.updateById(updateObj);
    }

   
    public void deleteSchedule(Long id) {
        // 校验存在
        validateScheduleExists(id);
        // 删除
        scheduleMapper.deleteById(id);
    }

    private void validateScheduleExists(Long id) {
        if (scheduleMapper.selectById(id) == null) {
            throw exception(SCHEDULE_NOT_EXISTS);
        }
    }

   
    public ScheduleDO getSchedule(Long id) {
        return scheduleMapper.selectById(id);
    }

   
    public List<ScheduleDO> getScheduleList(Collection<Long> ids) {
        return scheduleMapper.selectBatchIds(ids);
    }

   
    public PageResult<NickScheduleDO> getSchedulePage(SchedulePageReqVO pageReqVO) {
        Page<NickScheduleDO> list = scheduleMapper.list(new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize()), pageReqVO);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

   
    public List<ScheduleDO> getScheduleList(ScheduleExportReqVO exportReqVO) {
        return scheduleMapper.selectList(exportReqVO);
    }

}
